'use strict'
const path = require('path')

function resolve(dir){
  return path.join(__dirname, dir)
}
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const CompressionWebpackPlugin = require('compression-webpack-plugin')

const hosts = [
  'http://192.168.50.62:6060', // 默认开发环境
  'http://192.168.50.172:9909', // 费宁
  'http://192.168.50.157:9909' // 刘欢
]

// 在这里配置
module.exports = {
  productionSourceMap:false,
  // 基本路径
  publicPath:'./',
  // 输出文件目录
  outputDir:'local',
  // webpack-dev-server 相关配置
  devServer:{
    // port: 8888,
    proxy:{
      '/wms':{
        target:hosts[0],
        changeOrigin:true,
        pathRewrite:{
          '^wms':''
        }
      }
    }
  },
  chainWebpack:config => {

    // 移除prefetch插件，避免加载多余的资源
    config.plugins.delete('prefetch')
    // 移除 preload 插件，避免加载多余的资源
    config.plugins.delete('preload')

    config.optimization.minimize(true)

    config.optimization.splitChunks({
      chunks:'all',
      cacheGroups:{
        libs:{
          name:'chunk-libs',
          test:/[\\/]node_modules[\\/]/,
          priority:10,
          chunks:'initial' // only package third parties that are initially dependent
        },
        elementUI:{
          name:'chunk-elementUI', // split elementUI into a single package
          priority:20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
          test:/[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
        },
        commons:{
          name:'chunk-commons',
          test:resolve('src/components'), // can customize your rules
          minChunks:3, //  minimum common number
          priority:5,
          reuseExistingChunk:true
        }
      }
    })

    // config.module.rule('images')
    //   .test(/\.(png|jpe?g|gif|webp)(\?.*)?$/)
    //   .use('image-webpack-loader')
    //   .loader('image-webpack-loader')
    //   .options({
    //     bypassOnDebug:true
    //   })
    //   .end()

    // 展示打包图形化信息
    if (process.env.use_analyzer){
      config
        .plugin('webpack-bundle-analyzer')
        .use(BundleAnalyzerPlugin)
    }
  },
  configureWebpack:config => {
    if (process.env.NODE_ENV !== 'development'){
      config.output.filename = 'assets/[name].[hash:8].js'
      config.output.chunkFilename = 'assets/[name].[hash:8].js'
    }
    // 公共代码抽离
    config.optimization = {
      // 分割代码块
      splitChunks:{
        cacheGroups:{
          //公用模块抽离
          common:{
            chunks:'initial',
            minSize:0, //大于0个字节
            minChunks:2 //抽离公共代码时，这个代码块最小被引用的次数
          },
          //第三方库抽离
          vendor:{
            priority:1, //权重
            test:/node_modules/,
            chunks:'initial',
            minSize:0, //大于0个字节
            minChunks:2 //在分割之前，这个代码块最小应该被引用的次数
          }
        }
      }
    }
    // 开启gzip压缩
    // config.plugins.push(
    //   new CompressionWebpackPlugin(
    //     {
    //       filename:info => {
    //         return `${info.path}.gz${info.query}`
    //       },
    //       algorithm:'gzip',
    //       threshold:10240, // 只有大小大于该值的资源会被处理 10240
    //       test:new RegExp('\\.(' + ['js'].join('|') + ')$'
    //       ),
    //       minRatio:0.8, // 只有压缩率小于这个值的资源才会被处理
    //       deleteOriginalAssets:false // 删除原文件
    //     }
    //   )
    // )
  }
}